﻿using Microsoft.VisualBasic.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsStateService
{
    public class ServiceLogManager
    {
        private string _service_log_file_name;
        private static ServiceLogManager _manager;
        private static object fileLock = new object();

        public static ServiceLogManager Instance
        {
            get
            {
                if(_manager == null)
                {
                    _manager = new ServiceLogManager();
                }
                return _manager;
            }
        }

        public ServiceLogManager()
        {
            _service_log_file_name = CommonPath.ServiceLogFullFileName;
            if (!File.Exists(_service_log_file_name))
            {
                var stream = File.Create(_service_log_file_name);
                stream.Close();
            }
        }

        public void Error(string message, bool send_to_ui = true)
        {
            lock (fileLock)
            {
                using (FileStream fs = new FileStream(_service_log_file_name, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    string jsonString = JsonConvert.SerializeObject(new ServiceLogData() { DateTime = DateTime.Now, Content = message, LogLevel = System.Diagnostics.EventLogEntryType.Error });
                    byte[] bytes = Encoding.UTF8.GetBytes(jsonString + Environment.NewLine);
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
        }

        public void Warning(string message, bool send_to_ui = true)
        {
            lock (fileLock)
            {
                using (FileStream fs = new FileStream(_service_log_file_name, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    string jsonString = JsonConvert.SerializeObject(new ServiceLogData() { DateTime = DateTime.Now, Content = message, LogLevel = System.Diagnostics.EventLogEntryType.Warning });
                    byte[] bytes = Encoding.UTF8.GetBytes(jsonString + Environment.NewLine);
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
        }

        public void Info(string message, bool send_to_ui = true)
        {
            lock (fileLock)
            {
                using (FileStream fs = new FileStream(_service_log_file_name, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    string jsonString = JsonConvert.SerializeObject(new ServiceLogData() { DateTime = DateTime.Now, Content = message, LogLevel = System.Diagnostics.EventLogEntryType.Information });
                    byte[] bytes = Encoding.UTF8.GetBytes(jsonString + Environment.NewLine);
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
        }

        public void Debug(string message, bool send_to_ui = true)
        {
#if DEBUG
            lock (fileLock)
            {
                using (FileStream fs = new FileStream(_service_log_file_name, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    string jsonString = JsonConvert.SerializeObject(new ServiceLogData() { DateTime = DateTime.Now, Content = message, LogLevel = System.Diagnostics.EventLogEntryType.Information });
                    byte[] bytes = Encoding.UTF8.GetBytes(jsonString + Environment.NewLine);
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
#endif
        }
    }
}
